﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GA.Server.Api;

namespace GA.Server.Main.Actions
{
    public class StopServerAction : IConsoleAction
    {
        #region IConsoleAction Members

        public string Name
        {
            get { return "StopServer"; }
        }

        public string HelpInfo
        {
            get
            {
                StringBuilder help = new StringBuilder();
                help.AppendLine("StopServer Command:");
                help.AppendLine(" USAGE: StopServer [<ServerName>]");
                help.AppendLine(" DESCRIPTION: Stops the specified server.");
                help.AppendLine(" POSSIBLE ISSUE: Stopping a server while other servers rely on it may have drastic consequences.");
                return help.ToString();
            }
        }

        public ConsoleActionResult RunAction(string param)
        {
            GAServer server = Program.CurrentProgram.GetRunningServer(param);
            if (server == null 
                || server.Status == GAServerStatus.Initialized
                || server.Status == GAServerStatus.NotInitialized 
                || server.Status == GAServerStatus.Stopped
                || server.Status == GAServerStatus.Failed)
            {
                Console.WriteLine(string.Format("Server {0} is not running.", param));
                return ConsoleActionResult.Error;
            }
            if (server.Status == GAServerStatus.ShuttingDown)
            {
                Console.WriteLine(string.Format("Server {0} is already shutting down.", param));
                return ConsoleActionResult.Error;
            }
            server.Stop(false);
            Console.WriteLine(string.Format("Server {0} is shutting down.", param));
            return ConsoleActionResult.CompletedSuccessfully;
        }

        #endregion
    }
}
